<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>谈谈Spring中的BeanPostProcessor接口 | 博客主页 | 世界是个球，前方总有路！</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="Spring">
    <meta name="description" content="一、前言&amp;emsp;&amp;emsp;这几天正在复习Spring的相关内容，在了解bean的生命周期的时候，发现其中涉及到一个特殊的接口——BeanPostProcessor接口。由于网上没有找到比较好的博客，所有最后花了好几个小时，通过Spring的官方文档对它做了一个大致的了解，下面就来简单介绍一下这个接口。   二、正文2.1 BeanPostProcessor的功能&amp;emsp;&amp;emsp;有时候">
<meta property="og:type" content="article">
<meta property="og:title" content="谈谈Spring中的BeanPostProcessor接口">
<meta property="og:url" content="http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/index.html">
<meta property="og:site_name" content="博客主页">
<meta property="og:description" content="一、前言&amp;emsp;&amp;emsp;这几天正在复习Spring的相关内容，在了解bean的生命周期的时候，发现其中涉及到一个特殊的接口——BeanPostProcessor接口。由于网上没有找到比较好的博客，所有最后花了好几个小时，通过Spring的官方文档对它做了一个大致的了解，下面就来简单介绍一下这个接口。   二、正文2.1 BeanPostProcessor的功能&amp;emsp;&amp;emsp;有时候">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/1.png">
<meta property="article:published_time" content="2020-05-10T10:53:21.000Z">
<meta property="article:modified_time" content="2020-05-10T16:59:27.280Z">
<meta property="article:author" content="特务依昂">
<meta property="article:tag" content="Spring">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/1.png">
    
        <link rel="alternate" type="application/atom+xml" title="博客主页" href="/blog/atom.xml">
    
    <link rel="shortcut icon" href="/blog/img/title.png">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/blog/img/brand.jpg)">
      <div class="brand">
        <a href="/blog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">特务依昂</h5>
          <a href="mailto:1131564805@qq.com" title="1131564805@qq.com" class="mail">1131564805@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/blog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                博客
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/tewuyiang" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://weibo.com/u/5516635708/" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.cnblogs.com/tuyang1129/" target="_blank" >
                <i class="icon icon-lg icon-link"></i>
                博客园
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">谈谈Spring中的BeanPostProcessor接口</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">谈谈Spring中的BeanPostProcessor接口</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-05-10T10:53:21.000Z" itemprop="datePublished" class="page-time">
  2020-05-10
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/Spring/">Spring</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#一、前言"><span class="post-toc-number">1.</span> <span class="post-toc-text">一、前言</span></a></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#二、正文"><span class="post-toc-number">2.</span> <span class="post-toc-text">二、正文</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#2-1-BeanPostProcessor的功能"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">2.1 BeanPostProcessor的功能</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#2-2-BeanPostProcessor的使用"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">2.2 BeanPostProcessor的使用</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#2-3-一个简单的demo"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">2.3 一个简单的demo</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#2-4-使用BeanPostProcessor时容易踩的坑"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">2.4 使用BeanPostProcessor时容易踩的坑</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#三、总结"><span class="post-toc-number">3.</span> <span class="post-toc-text">三、总结</span></a></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#四、参考"><span class="post-toc-number">4.</span> <span class="post-toc-text">四、参考</span></a></li></ol>
        </nav>
    </aside>


<article id="post-谈谈Spring中的BeanPostProcessor接口"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">谈谈Spring中的BeanPostProcessor接口</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-05-10 18:53:21" datetime="2020-05-10T10:53:21.000Z"  itemprop="datePublished">2020-05-10</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/Spring/">Spring</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h1 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h1><p>&emsp;&emsp;这几天正在复习<code>Spring</code>的相关内容，在了解<code>bean</code>的生命周期的时候，发现其中涉及到一个特殊的接口——<code>BeanPostProcessor</code>接口。由于网上没有找到比较好的博客，所有最后花了好几个小时，通过<code>Spring</code>的官方文档对它做了一个大致的了解，下面就来简单介绍一下这个接口。</p>
<br>

<h1 id="二、正文"><a href="#二、正文" class="headerlink" title="二、正文"></a>二、正文</h1><h2 id="2-1-BeanPostProcessor的功能"><a href="#2-1-BeanPostProcessor的功能" class="headerlink" title="2.1 BeanPostProcessor的功能"></a>2.1 BeanPostProcessor的功能</h2><p>&emsp;&emsp;有时候，我们希望<code>Spring</code>容器在创建<code>bean</code>的过程中，能够使用我们自己定义的逻辑，对创建的<code>bean</code>做一些处理，或者执行一些业务。而实现方式有多种，比如自定义<code>bean</code>的初始化话方法等，而<code>BeanPostProcessor</code>接口也是用来实现类似的功能的。</p>
<p>&emsp;&emsp;如果我们希望容器中创建的每一个单例<code>bean</code>，在创建的过程中可以执行一些自定义的逻辑，那么我们就可以编写一个类，并让他实现<code>BeanPostProcessor</code>接口，然后将这个类注册到一个容器中。容器在创建<code>bean</code>的过程中，会优先创建实现了<code>BeanPostProcessor</code>接口的<code>bean</code>，然后，在创建其他<code>bean</code>的时候，会将创建的每一个<code>bean</code>作为参数，调用<code>BeanPostProcessor</code>的方法。而<code>BeanPostProcessor</code>接口的方法，即是由我们自己实现的。下面就来具体介绍一下<code>BeanPostProcessor</code>的使用</p>
<br>

<h2 id="2-2-BeanPostProcessor的使用"><a href="#2-2-BeanPostProcessor的使用" class="headerlink" title="2.2 BeanPostProcessor的使用"></a>2.2 BeanPostProcessor的使用</h2><p>&emsp;&emsp;我们先看一看<code>BeanPostProcessor</code>接口的代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">BeanPostProcessor</span> </span>&#123;</span><br><span class="line">	<span class="comment">// 注意这个方法名称关键的是before这个单词</span></span><br><span class="line">	<span class="function">Object <span class="title">postProcessBeforeInitialization</span><span class="params">(Object bean, String beanName)</span> </span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> BeansException</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 注意这个方法名称关键的是after这个单词</span></span><br><span class="line">	<span class="function">Object <span class="title">postProcessAfterInitialization</span><span class="params">(Object bean, String beanName)</span> </span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> BeansException</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;可以看到，<code>BeanPostProcessor</code>接口只有两个抽象方法，由实现这个接口的类去实现（后面简称这两个方法为<code>before</code>和<code>after</code>），这两个方法有着相同的参数：</p>
<ul>
<li><strong>bean</strong>：容器正在创建的那个<code>bean</code>的引用；</li>
<li><strong>beanName</strong>：容器正在创建的那个<code>bean</code>的名称；</li>
</ul>
<p>&emsp;&emsp;那这两个方法何时执行呢？这就涉及到<code>Spring</code>中，<code>bean</code>的生命周期了。下面引用<code>《Spring实战》</code>中的一张图，这张图表现了<code>bean</code>的生命周期，而<code>Spring</code>容器创建<code>bean</code>的具体过程，请参考这篇博客——<a href="https://www.cnblogs.com/tuyang1129/p/12861617.html" target="_blank" rel="noopener">简单谈谈Spring的IoC</a>。</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="1.png" alt="" title="">
                </div>
                <div class="image-caption"></div>
            </figure>

<p>&emsp;&emsp;上图中标红的两个地方就是<code>BeanPostProcessor</code>中两个方法的执行时机。<code>Spring</code>容器在创建<code>bean</code>时，如果容器中包含了<code>BeanPostProcessor</code>的实现类对象，那么就会执行这个类的这两个方法，并将当前正在创建的<code>bean</code>的引用以及名称作为参数传递进方法中。这也就是说，<code>BeanPostProcessor</code>的作用域是当前容器中的所有<code>bean</code>（不包括一些特殊的<code>bean</code>，这个后面说）。</p>
<p>&emsp;&emsp;值得注意的是，我们可以在一个容器中注册多个不同的<code>BeanPostProcessor</code>的实现类对象，而<code>bean</code>在创建的过程中，将会轮流执行这些对象实现的<code>before</code>和<code>after</code>方法。那执行顺序如何确定呢？<code>Spring</code>提供了一个接口<code>Ordered</code>，我们可以让<code>BeanPostProcessor</code>的实现类实现这个<code>Ordered</code>接口，并实现接口的<code>getOrder</code>方法。这个方法的返回值是一个<code>int</code>类型，<code>Spring</code>容器会通过这个方法的返回值，对容器中的多个<code>BeanPostProcessor</code>对象进行从小到大排序，然后在创建<code>bean</code>时依次执行它们的方法。也就是说，<code>getOrder</code>方法返回值越小的<code>BeanPostProcessor</code>对象，它的方法将越先被执行。</p>
<br>

<h2 id="2-3-一个简单的demo"><a href="#2-3-一个简单的demo" class="headerlink" title="2.3 一个简单的demo"></a>2.3 一个简单的demo</h2><p>&emsp;&emsp;下面就来写一个简单的<code>demo</code>，来看看<code>BeanPostProcessor</code>的效果。首先定义两个普通的<code>bean</code>，就叫<code>User</code>和<code>Car</code>吧：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line">	</span><br><span class="line">    <span class="comment">// ... 省略getter和setter...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> speed;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span> price;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// ... 省略getter和setter...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;在定义一个<code>BeanPostProcessor</code>的实现类，重写接口的方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PostBean</span> <span class="keyword">implements</span> <span class="title">BeanPostProcessor</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">postProcessBeforeInitialization</span><span class="params">(Object bean, String beanName)</span> </span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        <span class="comment">// 输出信息，方便我们看效果</span></span><br><span class="line">        System.out.println(<span class="string">"before -- "</span> + beanName);</span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">postProcessAfterInitialization</span><span class="params">(Object bean, String beanName)</span> </span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        <span class="comment">// 输出信息，方便我们看效果</span></span><br><span class="line">        System.out.println(<span class="string">"after -- "</span> + beanName);</span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;我们直接使用一个<code>Java</code>类作为<code>Spring</code>的配置，就不使用<code>xml</code>配置文件了。配置如下，在这个配置类中，声明了<code>User</code>、<code>Car</code>以及<code>PostBean</code>这三个<code>bean</code>的工厂方法，前两个是普通<code>bean</code>，而<code>PostBean</code>是实现<code>BeanPostProcessor</code>的<code>bean</code>：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BeanConfig</span> </span>&#123;</span><br><span class="line">	<span class="comment">// 在Spring中注册User这个bean</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> User <span class="title">user</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> User();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 在Spring中注册Car这个bean</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Car <span class="title">car</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Car();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在Spring中注册PostBean这个bean，这个bean实现了BeanPostProcessor接口</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> PostBean <span class="title">postBean</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> PostBean();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;好，有了上面四个类，就可以开始测试了，下面是测试方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testConfig</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ApplicationContext context =</span><br><span class="line">        <span class="keyword">new</span> AnnotationConfigApplicationContext(BeanConfig<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;上面这个方法啥也不干，就是创建一个<code>Spring</code>的上下文对象，也就是<code>Spring</code>的<code>IoC</code>容器。这个容器将去加载<code>BeanConfig</code>这个类的配置，然后创建配置类中声明的对象。在创建User和Car的过程中，就会执行<code>BeanPostProcessor</code>实现类的方法。我们看看执行结果：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">before -- org.springframework.context.event.internalEventListenerProcessor</span><br><span class="line">after -- org.springframework.context.event.internalEventListenerProcessor</span><br><span class="line">before -- org.springframework.context.event.internalEventListenerFactory</span><br><span class="line">after -- org.springframework.context.event.internalEventListenerFactory</span><br><span class="line">before -- car</span><br><span class="line">after -- car</span><br><span class="line">before -- user</span><br><span class="line">after -- user</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;可以看到，<code>BeanPostProcessor</code>的<code>before</code>方法和<code>after</code>方法都被调用了四次，最后两次调用时，传入的参数正是我们自己定义的<code>Bean</code>——<code>User</code>和<code>Car</code>。那为什么调用了四次呢，明明我们只定义了两个普通<code>bean</code>。我们看上面的输出发现，前两次调用，传入的<code>bean</code>是<code>Spring</code>内部的组件。<code>Spring</code>在初始化容器的过程中，会创建一些自己定义的<code>bean</code>用来实现一些功能，而这些<code>bean</code>，也会执行我们注册进容器中的<code>BeanPostProcessor</code>实现类的方法。</p>
<br>

<h2 id="2-4-使用BeanPostProcessor时容易踩的坑"><a href="#2-4-使用BeanPostProcessor时容易踩的坑" class="headerlink" title="2.4 使用BeanPostProcessor时容易踩的坑"></a>2.4 使用BeanPostProcessor时容易踩的坑</h2><p>&emsp;&emsp;<code>BeanPostProcessor</code>这个接口，在使用的过程中，其实还有许多的限制和坑点，若不了解的话，可能会让你对某些结果感到莫名其妙。下面我就来简单地说一说：</p>
<p><strong>（一）BeanPostProcessor依赖的bean，不会执行BeanPostProcessor的方法</strong></p>
<p>&emsp;&emsp;当我们在<code>BeanPostProcessor</code>的实现类中，依赖了其他的<code>bean</code>，那么被依赖的<code>bean</code>被创建时，将不会执行它所在的<code>BeanPostProcessor</code>实现类实现的方法，比如我们修改<code>PostBean</code>的实现，如下所示：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PostBean</span> <span class="keyword">implements</span> <span class="title">BeanPostProcessor</span>, <span class="title">Ordered</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 让PostBean依赖User</span></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> User user;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">postProcessBeforeInitialization</span><span class="params">(Object bean, String beanName)</span> </span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">postProcessAfterInitialization</span><span class="params">(Object bean, String beanName)</span> </span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;此时，容器在创建<code>User</code>这个<code>bean</code>时，不会执行<code>PostBean</code>实现的两个方法，因为由于<code>PostBean</code>依赖于<code>user</code>，所以<code>user</code>需要在<code>PostBean</code>之前创建完成，这也就意味着在<code>user</code>创建时，<code>PostBean</code>还未初始化完成，所以不会调用它的方法。</p>
<br>

<p><strong>（二）BeanPostProcessor以及依赖的bean无法使用AOP</strong></p>
<p>&emsp;&emsp;以下是<code>Spring</code>官方文档中的一段话：</p>
<blockquote>
<p>Because AOP auto-proxying is implemented as a <code>BeanPostProcessor</code> itself, neither <code>BeanPostProcessor</code> s nor the beans they reference directly are eligible for auto-proxying, and thus do not have aspects woven into them.</p>
</blockquote>
<p>&emsp;&emsp;上面这段话的意思大致是说，<code>Spring</code>的<code>AOP</code>代理就是作为<code>BeanPostProcessor</code>实现的，所以<strong>我们无法对BeanPostProcessor的实现类使用AOP织入通知，也无法对BeanPostProcessor的实现类依赖的bean使用AOP织入通知</strong>。<code>Spring</code>的<code>AOP</code>实现我暂时还没有研究过，所以上面的说<code>AOP</code>作为<code>BeanPostProcessor</code>实现的意思我不是特别明白，但是我们现在只需要关注<code>BeanPostProcessor</code>以及它依赖的<code>bean</code>都无法使用<code>AOP</code>这一点。为了验证上面的说法，我稍微修改一下<code>2.3</code>中的例子，来测试一波。</p>
<p>&emsp;&emsp;首先，我们修改<code>2.3</code>中用到的<code>PostBean</code>和<code>User</code>这两个类，让<code>PostBean</code>依赖<code>User</code>这个类，同时为了输出更加地简单，我们将<code>before</code>和<code>after</code>方法中的<code>println</code>语句删了：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PostBean</span> <span class="keyword">implements</span> <span class="title">BeanPostProcessor</span>, <span class="title">Ordered</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 让PostBean依赖User</span></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> User user;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">postProcessBeforeInitialization</span><span class="params">(Object bean, String beanName)</span> </span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">postProcessAfterInitialization</span><span class="params">(Object bean, String beanName)</span> </span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 此方法用来测试AOP，作为切点</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testAOP</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"Post Bean"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line">	</span><br><span class="line">    <span class="comment">// ... 省略getter和setter...</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 此方法用来测试AOP，用作切点</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testAOP</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"user bean"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;然后，我们定义一个<code>AOP</code>的切面，在切面中将<code>PostBean</code>的<code>testAOP</code>方法作为切点，代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BeanPostProcessorAspect</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">	<span class="comment">// 此方法织入PostBean的testAOP方法</span></span><br><span class="line">    <span class="meta">@Before</span>(<span class="string">"execution(* cn.tewuyiang.pojo.PostBean.testAOP(..))"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">before</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"before1"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 此方法织入User的testAOP方法</span></span><br><span class="line">    <span class="meta">@Before</span>(<span class="string">"execution(* cn.tewuyiang.pojo.User.testAOP(..))"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">before2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"before2"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;好，这就准备完毕，可以开始测试了。我们这次使用<code>Spring</code>注解扫描来配置<code>bean</code>以及为<code>bean</code>注入依赖，测试代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testConfig</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ApplicationContext context =</span><br><span class="line">        <span class="keyword">new</span> AnnotationConfigApplicationContext(AutoConfig<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">    <span class="comment">// 获取User这个bean，执行测试AOP的方法</span></span><br><span class="line">    User user = context.getBean(User<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">    user.testAOP();</span><br><span class="line">    <span class="comment">// 获取PostBean这个bean，执行测试AOP的方法</span></span><br><span class="line">    PostBean bean = context.getBean(PostBean<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">    bean.testAOP();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">输出如下：</span><br><span class="line">	user bean</span><br><span class="line">	post Bean</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;从输出中可以看到，使用<code>AOP</code>织入的前置通知没有执行，这也就验证了上面所说的，<code>BeanPostProcessor</code>的实现类以及实现类依赖的<code>bean</code>，无法使用<code>AOP</code>为其织入通知。但是这个限制具体有到什么程度，我也不是很确定，因为我使用<code>xml</code>配置依赖，以及上面使用注解扫描两种方式，<code>AOP</code>织入都没法使用，但是我在使用<code>@Bean</code>这种配置方式时，被依赖的<code>bean</code>却成功执行了通知。所以，关于此处提到的限制，还需要深入了解<code>Spring</code>容器的源码实现才能下定论。</p>
<br>

<p><strong>（三）注册BeanPostProcessor的方式以及限制</strong></p>
<p>&emsp;&emsp;我们如何将<code>BeanPostProcessor</code>注册到<code>Spring</code>容器中？方式主要有两种，第一种就是上面一直在用的，将其声明在<code>Spring</code>的配置类或<code>xml</code>文件中，作为普通的<code>bean</code>，让<code>ApplicationContext</code>对象去加载它，这样它就被自动注册到容器中了。而且<code>Spring</code>容器会对<code>BeanPostProcessor</code>的实现类做特殊处理，即会将它们挑选出来，在加载其他<code>bean</code>前，优先加载<code>BeanPostProcessor</code>的实现类。</p>
<p>&emsp;&emsp;还有另外一种方式就是使用<code>ConfigurableBeanFactory</code>接口的<code>addBeanPostProcessor</code>方法手动添加，<code>ApplicationContext</code>对象中组合了一个<code>ConfigurableBeanFactory</code>的实现类对象。但是这种方式添加<code>BeanPostProcessor</code>有一些缺点。首先，我们一创建<code>Spring</code>容器，在配置文件中配置的单例<code>bean</code>就会被加载，此时<code>addBeanPostProcessor</code>方法还没有执行，那我们手动添加的<code>BeanPostProcessor</code>也就无法作用于这些<code>bean</code>了，所以手动添加的<code>BeanPostProcessor</code>只能作用于那些延迟加载的<code>bean</code>，或者非单例<code>bean</code>。</p>
<p>&emsp;&emsp;还有一个就是，<strong>使用addBeanPostProcessor方式添加的BeanPostProcessor，Ordered接口的作用将失效，而是以注册的顺序执行</strong>。我们前面提过，<code>Ordered</code>接口用来指定多个<code>BeanPostProcessor</code>实现的方法的执行顺序。这是<code>Spring</code>官方文档中提到的：</p>
<blockquote>
<p>While the recommended approach for <code>BeanPostProcessor</code> registration is through <code>ApplicationContext</code> auto-detection (as described above), it is also possible to register them programmatically against a <code>ConfigurableBeanFactory</code> using the <code>addBeanPostProcessor</code> method. This can be useful when needing to evaluate conditional logic before registration, or even for copying bean post processors across contexts in a hierarchy. Note however that <code>BeanPostProcessor</code> s added programmatically do not respect the <code>Ordered</code> interface. Here it is the order of registration that dictates the order of execution. Note also that <code>BeanPostProcessor</code> s registered programmatically are always processed before those registered through auto-detection, regardless of any explicit ordering.</p>
</blockquote>
<br>

<p><strong>（四）使用@Bean配置BeanPostProcessor的限制</strong></p>
<p>&emsp;&emsp;如果我们使用<code>Java</code>类的方式配置<code>Spring</code>，并使用<code>@Bean</code>声明一个工厂方法返回<code>bean</code>实例，那么返回值的类型必须是<code>BeanPostProcessor</code>类型，或者等级低于<code>BeanPostProcessor</code>的类型。这里不好口头描述，直接看代码吧。以下是一个<code>BeanPostProcessor</code>的实现类，它实现了多个接口：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 此BeanPostProcessor的实现类，还实现了Ordered接口</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PostBean</span> <span class="keyword">implements</span> <span class="title">BeanPostProcessor</span>, <span class="title">Ordered</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">postProcessBeforeInitialization</span><span class="params">(Object bean, String beanName)</span> </span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"before -- "</span> + beanName);</span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">postProcessAfterInitialization</span><span class="params">(Object bean, String beanName)</span> </span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"after -- "</span> + beanName);</span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getOrder</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;我们在配置类中，声明<code>PostBean</code>可以有以下几种方式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BeanConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 方式1：PostBean</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> PostBean <span class="title">postBean</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> PostBean();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 方式2：返回值为BeanPostProcessor</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> BeanPostProcessor <span class="title">postBean</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> PostBean();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 方式3：返回值为Ordered</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Ordered <span class="title">postBean</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> PostBean();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;以上三种方式都可以让<code>Spring</code>容器创建<code>PostBean</code>实例对象，因为<code>PostBean</code>实现了<code>BeanPostProcessor</code>和<code>Ordered</code>接口，所以它也是这两种类型的对象。但是需要注意，上面三种方式中，只有第一种和第二种方式，会让<code>Spring</code>容器将<code>PostBean</code>当作<code>BeanPostProcessor</code>处理；而第三种方式，则会被当作一个普通<code>Bean</code>处理，实现<code>BeanPostProcessor</code>的两个方法都不会被调用。因为在<code>PostBean</code>的继承体系中，<code>Ordered</code>和<code>BeanPostProcessor</code>是同级别的，<code>Spring</code>无法识别出这个<code>Ordered</code>对象，也是一个<code>BeanPostProcessor</code>对象；但是使用<code>PostBean</code>却可以，因为<code>PostBean</code>类型就是<code>BeanPostProcessor</code>的子类型。<strong>所以，在使用@Bean声明工厂方法返回BeanPostProcessor实现类对象时，返回值必须是BeanPostProcessor类型，或者更低级的类型</strong>。<code>Spring</code>官方文档中，这一部分的内容如下：</p>
<blockquote>
<p>Note that when declaring a <code>BeanPostProcessor</code> using an <code>@Bean</code> factory method on a configuration class, the return type of the factory method should be the implementation class itself or at least the <code>org.springframework.beans.factory.config.BeanPostProcessor</code> interface, clearly indicating the post-processor nature of that bean. Otherwise, the <code>ApplicationContext</code> won’t be able to autodetect it by type before fully creating it. Since a <code>BeanPostProcessor</code> needs to be instantiated early in order to apply to the initialization of other beans in the context, this early type detection is critical.</p>
</blockquote>
<br>

<h1 id="三、总结"><a href="#三、总结" class="headerlink" title="三、总结"></a>三、总结</h1><p>&emsp;&emsp;以上就对<code>BeanPostProcessor</code>的功能、使用以及需要注意的问题做了一个大致的介绍。需要注意的是，上面所提到的问题，可能根据不同的情况，会有不同的结果，因为文档中的资料只是简单地提了几句，并不详细，上面的内容大部分都是我基于官方文档的描述，以及自己的测试得出，所以可能并不准确。还需要自己在实践中去尝试，或者阅读源码，才能彻底了解<code>BeanPostProcessor</code>的执行机制。</p>
<p>&emsp;&emsp;<strong>以上描述若存在错误或不足，希望能够提出来，因为这一部分内容，我也不太了解，所以希望有人帮忙指正</strong>。</p>
<br>

<h1 id="四、参考"><a href="#四、参考" class="headerlink" title="四、参考"></a>四、参考</h1><ul>
<li><a href="https://www.iteye.com/blog/chembo-858243" target="_blank" rel="noopener">https://www.iteye.com/blog/chembo-858243</a></li>
<li><a href="https://www.docs4dev.com/docs/en/spring-framework/4.3.21.RELEASE/reference/beans.html#beans-factory-extension-bpp" target="_blank" rel="noopener">Spring-4.3.21官方文档</a></li>
</ul>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-05-10T16:59:27.280Z" itemprop="dateUpdated">2020-05-11 00:59:27</time>
</span><br>


        
        世界是个球，前方总有路！
        
    </div>
    
    <footer>
        <a href="http://tewuyiang.gitee.io/blog">
            <img src="/blog/img/avatar.jpg" alt="特务依昂">
            特务依昂
        </a>
    </footer>
</blockquote>

        


        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/Spring/" rel="tag">Spring</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/&title=《谈谈Spring中的BeanPostProcessor接口》 — 博客主页&pic=http://tewuyiang.gitee.io/blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/&title=《谈谈Spring中的BeanPostProcessor接口》 — 博客主页&source=一个未来程序员的博客~~~" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《谈谈Spring中的BeanPostProcessor接口》 — 博客主页&url=http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/&via=http://tewuyiang.gitee.io/blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/%E6%B5%85%E6%9E%90Spring%E4%B8%ADbean%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">浅析Spring中bean的作用域</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/%E7%AE%80%E5%8D%95%E8%B0%88%E8%B0%88Spring%E7%9A%84IOC%E5%AE%9E%E7%8E%B0/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">简单谈谈Spring的IoC以及bean的生命周期</h4>
      </a>
    </div>
  
</nav>



    




















</article>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/blog/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>特务依昂 &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/&title=《谈谈Spring中的BeanPostProcessor接口》 — 博客主页&pic=http://tewuyiang.gitee.io/blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/&title=《谈谈Spring中的BeanPostProcessor接口》 — 博客主页&source=一个未来程序员的博客~~~" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《谈谈Spring中的BeanPostProcessor接口》 — 博客主页&url=http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/&via=http://tewuyiang.gitee.io/blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://tewuyiang.gitee.io/blog/%E8%B0%88%E8%B0%88Spring%E4%B8%AD%E7%9A%84BeanPostProcessor%E6%8E%A5%E5%8F%A3/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: false };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '人呢，怎么不见了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!欢迎回来!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
